//
//  AddressTableViewCell.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/9.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "AddressTableViewCell.h"

@interface AddressTableViewCell ()

@property (nonatomic, strong) UILabel *nameWithPhone;
@property (nonatomic, strong) UILabel *addressDetail;
@property (nonatomic, strong) UIImageView *line;
@property (nonatomic, strong) UIButton  * selectBtn;
@property (nonatomic, strong) UIButton *editBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation AddressTableViewCell

-(void)setAddressDataModel:(AddressManagerDataModel *)addressDataModel
{
    _addressDataModel = addressDataModel;
    
    _nameWithPhone.text = [[addressDataModel.consignee stringByAppendingString:@"  "] stringByAppendingString:addressDataModel.telephone];
    NSString *province = [addressDataModel.province stringByAppendingString:@" "];
    NSString *city = [addressDataModel.city stringByAppendingString:@" "];
    NSString *district = [addressDataModel.district stringByAppendingString:@" "];
    _addressDetail.text = [[[province stringByAppendingString:city] stringByAppendingString:district] stringByAppendingString:addressDataModel.place];
    if ([self.addressDataModel.Default isEqualToString:@"0"]) {
        [_selectBtn setTitle:@" 设为默认" forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];
    }else{
        [_selectBtn setTitle:@" 默认地址" forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"选择"] forState:UIControlStateNormal];
    }
}

+ (AddressTableViewCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"AddressTableViewCell";
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[AddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        [cell createSubView];
    }
    
    return cell;
}

- (void)createSubView {
    _nameWithPhone = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 16 * SCREEN_WIDTH / 375, SCREEN_WIDTH - 30 * SCREEN_WIDTH / 375, 16 * SCREEN_WIDTH / 375)];
    _nameWithPhone.textColor = FONTCOLOR_BLACK;
    _nameWithPhone.font = [UIFont systemFontOfSize:14 * SCREEN_WIDTH / 375];
    _nameWithPhone.text = @"林吃吃  18888888888";
    [self addSubview:_nameWithPhone];
    
    _addressDetail = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), 42 * SCREEN_WIDTH / 375, SCREEN_WIDTH - 30 * SCREEN_WIDTH / 375, 16 * SCREEN_WIDTH / 375)];
    _addressDetail.font = [UIFont systemFontOfSize:14 * SCREEN_WIDTH / 375];
    _addressDetail.textColor = [UIColor grayColor];
    _addressDetail.numberOfLines = 0;
    _addressDetail.text = @"浙江 杭州 西湖区 文三西路149号";
    [self addSubview:_addressDetail];
    
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(15 * SCREEN_WIDTH / 375, 71 * SCREEN_WIDTH / 375, SCREEN_WIDTH - 30 * SCREEN_WIDTH / 375, 0.5)];
    _line.image = [UIImage imageNamed:@"虚线"];
    [self addSubview:_line];
    
    self.selectBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(11), 83 * SCREEN_WIDTH / 375, 78 * SCREEN_WIDTH / 375, 17 * SCREEN_WIDTH / 375)];
    
    [_selectBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    _selectBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    
    [_selectBtn addTarget:self action:@selector(selectClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectBtn];
    
    _editBtn = [[UIButton alloc] initWithFrame:CGRectMake(250 * SCREEN_WIDTH / 375, 83 * SCREEN_WIDTH / 375, 50 * SCREEN_WIDTH / 375, 17 * SCREEN_WIDTH / 375)];
    [_editBtn setTitle:@" 编辑" forState:UIControlStateNormal];
    [_editBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    _editBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    [_editBtn setImage:[UIImage imageNamed:@"地址_编辑"] forState:UIControlStateNormal];
    [_editBtn addTarget:self action:@selector(editClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_editBtn];
    
    _deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(314 * SCREEN_WIDTH / 375, 83 * SCREEN_WIDTH / 375, 50 * SCREEN_WIDTH / 375, 17 * SCREEN_WIDTH / 375)];
    [_deleteBtn setTitle:@" 删除" forState:UIControlStateNormal];
    [_deleteBtn setTitleColor:FONTCOLOR_BLACK forState:UIControlStateNormal];
    _deleteBtn.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH / 375];
    [_deleteBtn setImage:[UIImage imageNamed:@"地址_删除"] forState:UIControlStateNormal];
    [_deleteBtn addTarget:self action:@selector(deleteClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteBtn];
}

- (void)selectClicked
{
    
    [_selectBtn setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    if (_selectClick) {
        _selectClick();
    }
}

- (void)editClicked
{
    if (_editClick) {
        _editClick();
    }
}

- (void)deleteClicked
{
    if (_deleteClick) {
        _deleteClick();
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

}

@end
